from rest_framework import serializers
from .models import *

class AdvSer(serializers.ModelSerializer):
    class Meta:
        model = AdvContent
        fields = "__all__"


class CategorySer(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = "__all__"


class Category2Ser(serializers.ModelSerializer):
    # 自定义一个字段 来表述  二级分类下的所有3级分类
    category3_list = serializers.SerializerMethodField()

    def get_category3_list(self, cate2):
        queryset = Category.objects.filter(parent_id=cate2.id)
        return CategorySer(queryset, many=True).data

    class Meta:
        model = Category
        fields = "__all__"


class ChannelSer(serializers.ModelSerializer):
    category1_list = serializers.SerializerMethodField()

    category2_list = serializers.SerializerMethodField()

    # category_set = CategorySer()
    # 获取 频道下所有的一级分类
    def get_category1_list(self, channel):
        # 根据 channel 对象 去查询 包含的所有分类
        queryset = Category.objects.filter(channel_id=channel.id, level=1)
        return CategorySer(queryset, many=True).data

    # 获取频道下所有的二级分类
    def get_category2_list(self, channel):
        # 根据 channel 对象 去查询 包含的所有分类
        queryset = Category.objects.filter(channel_id=channel.id,level=2)
        return Category2Ser(queryset, many=True).data

    class Meta:
        model = Channel
        fields = "__all__"

class GoodsListSer(serializers.ModelSerializer):
    class Meta:
        model = Goods
        fields = ['id','name','price','pic']

#创建一个分类商品组合序列化
class CategoryGoodsSer(serializers.ModelSerializer):
    #定义一个字段
    goods_list = serializers.SerializerMethodField()

    def get_goods_list(self,cate):
        queryset = Goods.objects.filter(category1=cate.id)
        return GoodsListSer(queryset,many=True).data
    class Meta:
        model = Category
        fields = '__all__'